/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package WebServices;

/**
 *
 * @author Sandino
 */
import Controladores.CtrlChat;
import DataTypes.DataMensaje;
import Interfaces.Factory;
import Interfaces.ICtrlChat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Logger;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.ParameterStyle;
import javax.jws.soap.SOAPBinding.Style;
import javax.xml.ws.Endpoint;

/**
 *
 * @author Sandino
 */
@WebService
@SOAPBinding(style = Style.RPC, parameterStyle = ParameterStyle.WRAPPED)

public class CtrlChatWS {
    
    private Endpoint endpoint = null;
    private Logger log = Logger.getLogger(CtrlChatWS.class.getName());
    private ICtrlChat ctrl = new CtrlChat();
    
    public void publicar() {
        Propiedades propiedades = Propiedades.getInstance();
        String nomCtrl = "CtrlChat";
        String camino = "http://" + propiedades.getServer() + ":" + propiedades.getPuerto() + "/" + nomCtrl;
        log.info("publicando el endpoint "+nomCtrl);
        endpoint = Endpoint.publish(camino, this);
        log.info("Endpoint "+nomCtrl+" publicado en " + camino);
    }

    @WebMethod(exclude = true)
    public Endpoint getEndpoint() {
        return endpoint;
    }
    
    @WebMethod
    public int InciarConv(@WebParam(name = "de")String e,@WebParam(name = "dr") String r){
        
        return ctrl.InciarConv(e, r);
    
    }
    
    @WebMethod
    public void Send(@WebParam(name = "dm")DataMensaje dm,@WebParam(name = "id") int id){
    
        ctrl.Send(dm, id);
        
    }
    
    @WebMethod
    public DataMensaje Recibe(@WebParam(name = "rcptr")String strUsr, @WebParam(name = "id") int id){
        
        return ctrl.Recibe(strUsr, id);
        
    }
    @WebMethod
    public String[] getParticipantes(@WebParam(name = "idChat")String idChat){
        
        return ctrl.getParticipantes(idChat);
        
    }
    @WebMethod
    public void agregarAmigoAlChat(@WebParam(name = "id")int id,@WebParam(name = "invitado") String invitado){
         
         ctrl.agregarAmigoAlChat(id, invitado);
         
     }
    @WebMethod
    public void abandonarChat(@WebParam(name = "id")int id,@WebParam(name = "clave") String clave){
         
         ctrl.abandonarChat(id, clave);
         
     }
    @WebMethod
    public DataMensaje[] getHistorial(String idChat){
        
        ArrayList<DataMensaje> historial = ctrl.getHistorial(idChat);
        Iterator<DataMensaje> it = historial.iterator();
        
        DataMensaje [] res = new DataMensaje[historial.size()];
        int i = 0;
        while(it.hasNext()){
            
            res[i]= it.next();
            i++;
        }
        return res;
        }
    
    @WebMethod
    public boolean finalizo(String idChat){
        
       CtrlChat ch = new CtrlChat();
       return ch.finalizoChat(idChat);
        
    }        
    @WebMethod
    public void enviarBandejaJpa(DataMensaje[] list,int idChat){
        
        int tam = list.length;
        while(tam!=0){
            
        ctrl.Send(list[tam-1], idChat);
            
        }
        
    }
    
}
